package com.itheima.vms.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.vms.domain.ResponseResult;
import com.itheima.vms.domain.po.ItemCategory;
import com.itheima.vms.response.Result;
import com.itheima.vms.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类表 前端控制器
 * </p>
 *
 * @author shenkunlin
 * @since 2025-08-02
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

//    @GetMapping("/list")
//    public ResponseResult<List<ItemCategory>> list(
//            @RequestParam(defaultValue = "1") Integer page,
//            @RequestParam(defaultValue = "10") Integer pageSize) {
//        // 分页处理
//        PageHelper.startPage(page, pageSize);
//        List<ItemCategory> list = categoryService.list();
//        // 使用 PageInfo 获取分页信息
//        PageInfo<ItemCategory> pageInfo = new PageInfo<>(list);
//        return new ResponseResult<>(200, "操作成功", pageInfo.getList());
//    }

    @GetMapping("/list")
    public ResponseResult<List<ItemCategory>> getCategoryList(){
        List<ItemCategory> categories = categoryService.listAll();
        return ResponseResult.success(categories);
    }

    //新增
    @PostMapping
    public Result<Object> addCategory(@RequestBody ItemCategory category){
        boolean save = categoryService.save(category);
        return Result.ok(save);
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result<ItemCategory> queryCategoryByIdApi(@PathVariable Long id){
        ItemCategory category = categoryService.getById(id);
        return Result.ok(category);
    }
    //修改
    @PutMapping
    public Result<Object> updateCategory(@RequestBody ItemCategory category){
        categoryService.updateById(category);
        return Result.ok();
    }
    //删除
    @DeleteMapping("/{id}")
    public Result<Object> deleteCategory(@PathVariable Long id){
        categoryService.removeById(id);
        return Result.ok();
    }

}
